package system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import system.dao.ResourceDao;
import system.dao.SyroleSyresourcesDao;
import system.httpModel.Resources;
import system.model.Syresources;
import system.model.SyroleSyresources;
import system.service.ResourcesService;
import system.util.ResourcesComparator;

import common.httpModel.EasyuiTreeNode;
import common.service.impl.BaseServiceImpl;

@Service("resourcesService")
public class ResourceServiceImpl extends BaseServiceImpl implements ResourcesService{
	private ResourceDao<Syresources> resourceDao;
	private SyroleSyresourcesDao<SyroleSyresources> syroleSyresourcesDao;
	
	
	public SyroleSyresourcesDao<SyroleSyresources> getSyroleSyresourcesDao() {
		return syroleSyresourcesDao;
	}
	@Autowired
	public void setSyroleSyresourcesDao(
			SyroleSyresourcesDao<SyroleSyresources> syroleSyresourcesDao) {
		this.syroleSyresourcesDao = syroleSyresourcesDao;
	}

	public ResourceDao<Syresources> getResourceDao() {
		return resourceDao;
	}

	@Autowired
	public void setResourceDao(ResourceDao<Syresources> resourceDao) {
		this.resourceDao = resourceDao;
	}


	public List<EasyuiTreeNode> tree(String id) { 
		String hql="from Syresources t where t.syresources is null order by t.seq";
		if(id!=null&&!id.trim().equals("")){
			hql="from Syresources t where t.syresources.id='"+id+"' order  by t.seq";
		}
		List<Syresources> resourceses=resourceDao.find(hql);
		List<EasyuiTreeNode> tree=new ArrayList<EasyuiTreeNode>();
		for(Syresources syresources: resourceses){
			tree.add(tree(syresources,false));
		}
		
		return tree;
	}

	private EasyuiTreeNode tree(Syresources syresources, boolean recursive) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(syresources.getId());
		node.setText(syresources.getText());
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("src", syresources.getSrc());
		node.setAttributes(attributes);
		if (syresources.getSyresourceses() != null && syresources.getSyresourceses().size() > 0) {
			node.setState("closed");
			if (recursive) {// 递归查询子节点
				List<Syresources> syresourcesList = new ArrayList<Syresources>(syresources.getSyresourceses());
				Collections.sort(syresourcesList, new ResourcesComparator());// 排序
				List<EasyuiTreeNode> children = new ArrayList<EasyuiTreeNode>();
				for (Syresources r : syresourcesList) {
					EasyuiTreeNode t = tree(r, true);
					children.add(t);
				}
				node.setChildren(children);
			}
		}
		return node;
	}
	@Transactional(readOnly = true)
	public List<Resources> treegrid(String id) {
		List<Resources> treegrid = new ArrayList<Resources>();
		String hql = "from Syresources t where t.syresources is null order by t.seq";
		if (id != null && !id.trim().equals("")) {
			hql = "from Syresources t where t.syresources.id = '" + id + "' order by t.seq";
		}
		List<Syresources> syresourceses = resourceDao.find(hql);
		for (Syresources syresources : syresourceses) {
			Resources r = new Resources();
			BeanUtils.copyProperties(syresources, r);
			if (syresources.getSyresources() != null) {
				r.setParentId(syresources.getSyresources().getId());
				r.setParentText(syresources.getSyresources().getText());
			}
			if (syresources.getSyresourceses() != null && syresources.getSyresourceses().size() > 0) {
				r.setState("closed");
			}
			treegrid.add(r);
		}
		return treegrid;
	}

	public Resources add(Resources resources) {
		Syresources syresources = new Syresources();
		BeanUtils.copyProperties(resources, syresources);
		if (resources.getParentId() != null && !resources.getParentId().trim().equals("")) {
			syresources.setSyresources(resourceDao.load(Syresources.class, resources.getParentId()));
		}
		if(syresources.getId()==null){
			syresources.setId(UUID.randomUUID().toString());
		}
		resourceDao.save(syresources);
		return resources;
	}

	public boolean del(Resources resources) {
		Syresources syresources =null;
		if(resources.getId()!=null){
			 syresources = resourceDao.get(Syresources.class, resources.getId());
		}else if(resources.getMenuId()!=null){
			syresources = resourceDao.find("from Syresources sr where sr.menuId=?", resources.getMenuId()).get(0);
		}

		if (resources != null) {
			return recursiveDelete(syresources);
		}
		
		return true;
	}
	private boolean recursiveDelete(Syresources syresources) {
		if (syresources.getSyresourceses() != null && syresources.getSyresourceses().size() > 0) {
			Set<Syresources> syresourceses = syresources.getSyresourceses();
			if(syresourceses.size()>0){
				return false;
			}
//			for (Syresources t : syresourceses) {//批量删除有问题
//				recursiveDelete(t);
//			}
		}

		List<SyroleSyresources> syroleSyresourcesList = syroleSyresourcesDao.find("from SyroleSyresources t where t.syresources=?", syresources);
		if (syroleSyresourcesList != null) {// 删除资源前,需要现将角色资源关系表中的相关记录删除
			for (SyroleSyresources syroleSyresources : syroleSyresourcesList) {
				syroleSyresourcesDao.delete(syroleSyresources);
			}
		}

		resourceDao.delete(syresources);
		
		return true;
	}
	public Resources edit(Resources resources) {
		Syresources syresources =null;
		if(resources.getId()!=null){
			 syresources = resourceDao.get(Syresources.class, resources.getId());
		}else if(resources.getMenuId()!=null){
			syresources = resourceDao.find("from Syresources sr where sr.menuId=?", resources.getMenuId()).get(0);
		}
	
		if (syresources != null) {
			String id=syresources.getId();
			BeanUtils.copyProperties(resources, syresources);
			syresources.setId(id);
			if(resources.getParentId()!=null){
				if (!syresources.getId().equals("0")) {// 跟节点不可以修改上级节点
					syresources.setSyresources(resourceDao.get(Syresources.class, resources.getParentId()));
				}
			}
		}
		return resources;
	}
	
	public void queryResources(String id) {
		Syresources sy=resourceDao.find("from Syresources sr where sr.menuId=?", id).get(0);
		
		if(sy!=null){
			List<Syresources> syresources =resourceDao.find("from Syresources sr where sr.syresources.id=? ", sy.getId());
			for(Syresources s:syresources){
				s.setSyresources(null);
			}
		}

	}
}
